package com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyActivity;

import android.app.Activity;
import android.content.Context;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import com.diudiutechnology.www.yadiudiu.util.http.HttpClient;
import com.diudiutechnology.www.yadiudiu.util.http.JsonTools;
import com.diudiutechnology.www.yadiudiu.util.utils.MyProgressDialog;
import com.diudiutechnology.www.yadiudiu.util.utils.Sundry;
import com.diudiutechnology.www.yadiudiu.util.utils.ToastUtils;
import com.diudiutechnology.www.yadiudiu.util.view.pulltorefresh.PullToRefreshBase;
import com.diudiutechnology.www.yadiudiu.util.view.pulltorefresh.PullToRefreshListView;
import com.diudiutechnology.www.yadiudiu.yadiudiu.main.MyActivity.LoginActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyAdapter.SignIntegralActivityAdapter;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.RegistrationModel;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.SignIntegralModel;
import com.diudiutechnology.www.yadiudiu.R;

import org.xutils.common.Callback;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/8/29.
 * 积分签到
 */

public class SignIntegralActivity extends Activity implements View.OnClickListener ,Callback.CommonCallback<String>{
    @Bind(R.id.retur)
    LinearLayout retur;


    @Bind(R.id.available_layout)//无消息提示
            LinearLayout available_layout;
    ListView listview;
    @Bind(R.id.listview)
    PullToRefreshListView xlistview;

    @Bind(R.id.signintegral_text)//总积分
    TextView signintegral_text;

    @Bind(R.id.signintegral_text1)
    TextView signintegral_text1;
    @Bind(R.id.signintegral_text2)
    TextView signintegral_text2;

    @Bind(R.id.add)
    TextView add;

    List<SignIntegralModel> getinfo=new ArrayList<>();
    SignIntegralActivityAdapter adapter;

    // 请求数据
    private int currentPage = 1;

    private boolean mIsStart = true;//判断是否上下加载 true:下拉刷新  false:上拉加载数据

    Context context;
    private MyProgressDialog mDialog;
    String Judge_interface="0";//判断哪个接口   0、获取详情  1、签到
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_integral);
        ButterKnife.bind(this);
        context=this;
        initView();
    }

    private void initView() {
        retur.setOnClickListener(this);
        signintegral_text2.setOnClickListener(this);

        listview=xlistview.getRefreshableView();
        // 显示头部出现分割线
        listview.setHeaderDividersEnabled(false);
        // 禁止底部出现分割线
        listview.setFooterDividersEnabled(false);
        listview.setDivider(new ColorDrawable(Color.TRANSPARENT));
        listview.setDividerHeight(0);
        xlistview.setOnRefreshListener(refreshListener);
        adapter = new SignIntegralActivityAdapter(context);
        listview.setAdapter(adapter);

        processpData();

    }
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.retur:
                finish();
                break;
            case R.id.signintegral_text2:
                processpSignIntegra();
                break;



        }
    }

    private void setLastUpdateTime() {
        String text = formatDateTime(System.currentTimeMillis());
        xlistview.setLastUpdatedLabel(text);
    }
    /**
     * 上拉刷新，下拉获取更多
     */
    PullToRefreshBase.OnRefreshListener refreshListener = new PullToRefreshBase.OnRefreshListener() {

        @Override
        public void onPullDownToRefresh(PullToRefreshBase refreshView) {
            mIsStart = true;
            currentPage = 1;
            if (getinfo!=null){
                getinfo.clear();
            }
            if (adapter!=null){
                adapter.emptying();
            }
            processpData();
        }

        @Override
        public void onPullUpToRefresh(PullToRefreshBase refreshView) {
            mIsStart = false;
            currentPage++;
            processpData();
        }
    };
    private String formatDateTime(long time) {
        if (0 == time) {
            return "";
        }
        SimpleDateFormat mDateFormat = new SimpleDateFormat("MM-dd HH:mm");
        return mDateFormat.format(new Date(time));
    }

    //签到详情页面
    private void processpData(){
        if (Sundry.isOpenNetwork(context)){
            mDialog =new MyProgressDialog(context);
            Judge_interface="0";
            HttpClient.signinDetails(""+currentPage,this);
        }else {
            ToastUtils.Toastshow(context, getResources().getString(R.string.network));
        }
    }
    //签到
    private void processpSignIntegra(){
        if (Sundry.isOpenNetwork(context)){
            mDialog =new MyProgressDialog(context);
            Judge_interface="1";
            HttpClient.userSign(this);
        }else {
            ToastUtils.Toastshow(context, getResources().getString(R.string.network));
        }
    }
    @Override
    public void onSuccess(String result) {
        if (mDialog!=null){
            mDialog.dismiss();
        }
        System.out.println("stringmd5result"+result);
        List<RegistrationModel> getnote = JsonTools.getunderlying(result);
        if (getnote!=null||getnote.size()>0){
            String status = getnote.get(0).getStatus();
            String getMsg = getnote.get(0).getMsg();
            if (status.equals("200")){
                if (Judge_interface.equals("0")){
                    String content = getnote.get(0).getContent();
                    String sign = getnote.get(0).getSign();
                    String signintegral = getnote.get(0).getSignintegral();
                    String userScore = getnote.get(0).getUserScore();
                    if (sign.equals("1")){
                        signintegral_text1.setText("已签到");
                        add.setText("+"+signintegral);
                        signintegral_text2.setText("已签到");
                    }else {
                        signintegral_text1.setText("未签到");
                        signintegral_text2.setText("签到");
                    }
                    signintegral_text.setText("我的积分:"+userScore);
                    if(!Sundry.getStinges(content)){
                        getinfo = JsonTools.getSignIntegral(content);
                        for(int i=0;i<getinfo.size();i++){//存入数据
                            adapter.addItem(getinfo.get(i),0);
                        }
                        if (adapter.number()>7){//大于7条时可以上拉加载
                            xlistview.setPullLoadEnabled(false);
                            xlistview.setScrollLoadEnabled(true);
                        }
                        available_layout.setVisibility(View.GONE);
                    }else {
                        ToastUtils.show(context, getMsg);
                    }
                }else {
                    if (getinfo!=null){
                        getinfo.clear();
                    }
                    if (adapter!=null){
                        adapter.emptying();
                    }
                    processpData();
                    ToastUtils.show(context, getMsg);
                }

            }else if (status.equals("105")){
                Intent intent = new Intent(context, LoginActivity.class);
                startActivity(intent);
                ToastUtils.show(context, getMsg);
                if (Judge_interface.equals("0")){
                    available_layout.setVisibility(View.VISIBLE);
                }
            }else {
                if (Judge_interface.equals("0")){
                    available_layout.setVisibility(View.VISIBLE);
                }
                ToastUtils.show(context, getMsg);
            }
        }
        if (Judge_interface.equals("0")){
            xlistview.onPullDownRefreshComplete();
            xlistview.onPullUpRefreshComplete();
            setLastUpdateTime();

        }
    }
    @Override
    public void onError(Throwable ex, boolean isOnCallback) {
        if (mDialog!=null){
            mDialog.dismiss();
        }
        if (Judge_interface.equals("0")){
            xlistview.onPullDownRefreshComplete();
            xlistview.onPullUpRefreshComplete();
            setLastUpdateTime();
            available_layout.setVisibility(View.VISIBLE);
        }
        System.out.println("stringmd5result"+ex.getMessage().toString());
        ToastUtils.Toastshow(context, getResources().getString(R.string.server));
    }

    @Override
    public void onCancelled(CancelledException cex) {
        if (mDialog!=null){
            mDialog.dismiss();
        }
        if (Judge_interface.equals("0")){
            xlistview.onPullDownRefreshComplete();
            xlistview.onPullUpRefreshComplete();
            setLastUpdateTime();
            available_layout.setVisibility(View.VISIBLE);
        }
        System.out.println("stringmd5result"+cex.getMessage().toString());
        ToastUtils.Toastshow(context, getResources().getString(R.string.server));
    }

    @Override
    public void onFinished() {

    }
}

